<?php
class Upload extends Action implements IUploader {
	private $errors = array (1 => '图片大小超出PHP限制！', 2 => '图片大小超出HTML限制！', 3 => '文件只有部分被上传！', 4 => '没有文件被上传！', 6 => '找不到临时文件夹！', 7 => '临时目录满，文件写入失败！' ); //错误信息
	private $error = null;
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		$I  = check_login ( ADMIN_LOGIN_PAGE );
		//文件上传状态,当成功时返回SUCCESS
		$state = "SUCCESS";
		$title = '';
		$picurl = false;
		if (! empty ( $_FILES ['picdata'] ) && $_FILES ['picdata'] ['error'] == 0) {
			$title = htmlspecialchars ( $_POST ['pictitle'], ENT_QUOTES );
			$uploader = apply_filter ( 'get_uploader', $this ); //得到文件上传器			
			$file = $_FILES ["picdata"];
			//格式验证			
			$current_type = strtolower ( strrchr ( $file ["name"], '.' ) );
			if (! $uploader->allowed ( $current_type )) {
				$state = "不支持的图片类型！";
			}
			//大小验证
			$file_size = 1024 * $uploader->getMaxSize ();
			if ($file ["size"] > $file_size) {
				$state = "图片大小超出限制！";
			}
			//保存图片
			if ($state == "SUCCESS" && is_uploaded_file ( $file ['tmp_name'] )) {
				$picurl = $uploader->save ( $file );
				if ($picurl === false) {
					$state = $uploader->get_last_error ();
				} else { //保存成功时需要将附件写入数据库					
					$atM = new WebAttachmentModel ();
					$data ['create_uid'] = $I->uid;
					$data ['create_time'] = time ();
					$data ['type'] = UploadTmpFile::getAttachmentType ( $current_type );
					$data ['mine_type'] = UploadTmpFile::getAttachmentMimeType ( $current_type );
					$data ['url'] = $picurl [0];
					$data ['name'] = $title;
					$data ['ext'] = trim ( $current_type, '.' );
					$data ['alt_text'] = $title;
					$atM->save ( $data );
					//生成缩略图与添加水印
					if (in_array ( $current_type, array ('.jpg', '.gif', 'jpeg', 'png' ) )) {
						$imageUtil = new ImageUtil ( $picurl [1] );
						$imageUtil->thumbnail ( array (array (80, 60 ), array (280, 180 ) ) );
					}
				}
			}
		} else if (! empty ( $_FILES ['picdata'] )) {
			$state = $this->errors [$_FILES ['picdata'] ['error']];
		} else {
			$state = '数据格式不正确。';
		}
		return new JsonView ( array ('url' => $picurl [0], 'title' => $title, 'state' => $state ) );
	
	}
	/* 默认文件上传器
	 * @see IUploader::save()
	 */
	public function save($file) {
		$path = (defined ( 'UPLOAD_DIR' ) && UPLOAD_DIR ? UPLOAD_DIR : 'uploads') . date ( '/Y/m/' );
		$tmp_file = $file ["name"];
		$destdir = WWW_ROOT . DS . $path;
		if (! is_dir ( $destdir ) && ! @mkdir ( $destdir, 0777, true )) {
			$this->error = "上传文件失败，无法创建目录:{$destdir}";
			elog ( $this->error );
			return false;
		}
		$fileName = rand ( 1, 10000 ) . time () . strrchr ( $tmp_file, '.' );
		$destfile = $destdir . $fileName;
		$result = move_uploaded_file ( $file ["tmp_name"], $destfile );
		
		if (! $result) {
			$this->error = '上传文件失败,无法移动上传的文件到：' . $destfile;
			elog ( $this->error );
			return false;
		}
		return array (str_replace ( DS, '/', $path . $fileName ), $destfile );
	}
	/**
	 * 
	 * 允许的文件类型
	 * @return array
	 */
	public function allowed($ext) {
		return in_array ( $ext, array (".gif", ".png", ".jpg", ".jpeg", ".bmp" ) );
	}
	/**
	 * 
	 * 允许的最大路径
	 * @return INT 单位为K
	 */
	public function getMaxSize() {
		return 2048;
	}
	public function get_last_error() {
		return $this->error;
	}
}